/**
 * 修改提交参数的key
 * @param src
 */
function processParam(src) {
    var data = {};
    for(var k in src){
        if(src[k] instanceof Object){
            for(var k1 in src[k]){
                data[k+"."+k1]=src[k][k1];
            }
        }else{
            data[k] = src[k];
        }
    }
    return data;
}

var isAdd = true;
var vm = new Vue({
    el:"#main",
    data:{
        search:{
            current: 1,
            size: 10,
            ultimate:"",
            elementary:"",
            categories:[],
        },
        categories:[],
        linkCategories:[],
        total:0,
        listLoading:true,
        tableHeight:"",
        tableData:[],
        addVisible:false,
        editDisabled:false,
        form:{
            id:"",
            ultimateCode:"",
            elementaryCode:"",
            dunsUltimate:{
                id:"",
                ultimateName:"",
                ultimateLinkCategory:""
            },
            dunsElementary:{
                id:"",
                elementaryName:"",
                elementaryLinkCategory:""
            }
        },
        dialogTitle:""
    },
    methods:{
        getAllCategory:function(){
            VanyAjax("/data/categoryList/queryByLevel",{"level":'Category'}, "POST").done(function (data) {
                this.categories=data;
            }, false, this);
        },
        selectAllLinkCategory:function () {
            VanyAjax("/data/duns/category",{}, "GET").done(function (data) {
                this.linkCategories=data;
            }, false, this);
        },
        export:function(){
            VanyAjax("/data/plant/list", {
                current: this.pageIndex,
                size: this.pageSize,
                key: this.search,
                export:true
            }, "POST").done(function (data) {

            }, false, this);
        },
        getData:function () {
            this.listLoading = true;
            VanyAjax("/data/duns/list", this.search, "POST").done(function (data) {
                this.total = data.total;
                this.tableData=data.records;
                this.listLoading = false;
            }, false, this);
        },
        submitForm:function () {
            var url = isAdd?"/data/duns/save":"/data/duns/update";
            VanyAjax(url,processParam(this.form), "POST").done(function (data) {
               this.addVisible=false;
               this.getData();
            }, true, this);
        },
        add:function(){
            this.dialogTitle=$("#addTitle").val();
            this.editDisabled=false;
            isAdd = true;
            this.form.id="";
            this.form.ultimateCode="";
            this.form.elementaryCode="";
            this.form.dunsUltimate.id="";
            this.form.dunsUltimate.ultimateName="";
            this.form.dunsUltimate.ultimateLinkCategory="";
            this.form.dunsElementary.id="";
            this.form.dunsElementary.elementaryName="";
            this.form.dunsElementary.elementaryLinkCategory="";
            this.addVisible=true;
        },
        edit:function (row) {
            this.dialogTitle=$("#editTitle").val();
            this.editDisabled=true;
            isAdd = false;
            this.form.id=row.id;
            this.form.ultimateCode=row.ultimateCode;
            this.form.elementaryCode=row.elementaryCode;
            this.form.dunsUltimate.id=row.dunsUltimate.id;
            this.form.dunsUltimate.ultimateName=row.dunsUltimate.ultimateName;
            this.form.dunsUltimate.ultimateLinkCategory=row.dunsUltimate.ultimateLinkCategory;
            this.form.dunsElementary.id=row.dunsElementary.id;
            this.form.dunsElementary.elementaryName=row.dunsElementary.elementaryName;
            this.form.dunsElementary.elementaryLinkCategory=row.dunsElementary.elementaryLinkCategory;
            this.addVisible=true;
        },
        upload:function () {

        },
        delete:function (row) {
            if(row.id==null){
                return;
            }
            this.$confirm('Confirm deletion?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(function() {
                VanyAjax("/data/duns/delete",{"id":row.id}, "POST").done(function (data) {
                    vm.getUserData();
                }, true, this);
            }).catch(function() {})
        }
    },
    mounted:function () {
        this.tableHeight = window.innerHeight - 450;
        this.getData();
        this.getAllCategory();
        this.selectAllLinkCategory();
    }
})
